export type MemberDynamicBonu = {
  mode: 'amount' | 'percent' // 计算模式
  value: number // 值
  delay: number // 延迟天数
}

export type MemberFixedBonu = MemberDynamicBonu & {
  name: string // 分账名称
  type: 'MERCHANT_ID' | 'PERSONAL_OPENID' | 'OFFLINE' // 分账类型
  account: string // 分账账户
  // relationType: 'SUPPLIER' | 'USER'
}

export const statusDict = [
  { label: '上架', value: 1, type: 'success' },
  { label: '下架', value: 2, type: 'info' }
]

export const bindTypeDict = [
  { label: '课程', value: 1, type: 'warning' },
  { label: '服务', value: 2, type: '' },
  { label: '班型', value: 3, type: 'success' }
]

export const statusMap = statusDict.reduce((prev, curr) => {
  prev[curr.value] = curr
  return prev
}, {})

export const bindTypeMap = bindTypeDict.reduce((prev, curr) => {
  prev[curr.value] = curr
  return prev
}, {})

export const mode2TextMap: Record<MemberDynamicBonu['mode'], string> = {
  amount: '金额',
  percent: '百分比'
}

export const mode2UnitMap: Record<MemberDynamicBonu['mode'], string> = {
  amount: '分',
  percent: '%'
}

export const modeAndValueRules = (currentMode: MemberDynamicBonu['mode']) => ({
  mode: [{ required: true, message: '请选择分润模式', trigger: 'blur' }],
  value: [
    { required: true, message: `请输入分润${mode2TextMap[currentMode]}`, trigger: 'blur' },
    {
      validator: (_, value: string) => {
        const num = +value
        if (currentMode === 'percent') {
          if (num > 100) return Promise.reject(new Error('分润百分比不能大于 100'))
          if (num < 0) return Promise.reject(new Error('分润百分比不能小于 0'))
        } else if (currentMode === 'amount') {
          if (num < 0) return Promise.reject(new Error('分润金额不能小于 0'))
          if (num > 1000) return Promise.reject(new Error('分润金额不能大于 1000'))
        }
        return Promise.resolve()
      },
      trigger: 'blur'
    }
  ]
})
